package com.example.administrator.yunniuusedcar.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.yunniuusedcar.adapter.ContactAdapter;
import com.example.administrator.yunniuusedcar.model.Contact;
import com.example.administrator.common.utils.Constant;
import com.example.administrator.yunniuusedcar.view.LetterBar;
import com.example.administrator.yunniuusedcar.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 *
 * 选车   --》更多功能
 */
    // 选择 车型  更多

public class ChooseCarModelsActivity extends Activity {

    private TextView tvLetter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_car_models);
        tvLetter = (TextView) findViewById(R.id.tv_letter);

        initLetterBar();
        initListView();
    }

    ArrayList<Contact> listDatas;

    private void initListView() {
        listView = (ListView) findViewById(R.id.list_view);

        // 准备列表数据
        listDatas = new ArrayList<>();
        for (String name : Constant.LIST_DATAS2) {
            Contact contact = new Contact(name);
            listDatas.add(contact);
        }

        // 排序操作
        Collections.sort(listDatas, new Comparator<Contact>() {
            @Override
            public int compare(Contact o1, Contact o2) {
                // 按拼音升序, 取负号则为降序
                return o1.pinyin.compareTo(o2.pinyin);
            }
        });

        // 显示列表
        ContactAdapter contactAdapter = new ContactAdapter(this, listDatas);
        listView.setAdapter(contactAdapter);
    }

    private void initLetterBar() {
        LetterBar letterBar = (LetterBar) findViewById(R.id.letter_bar);

        letterBar.setTextView(tvLetter);

        letterBar.setOnLetterSelectedListener(new LetterBar.OnLetterSelectedListener() {
            @Override // 用户选中某个字母
            public void onLetterSelected(String letter, int index) {
                // System.out.println("-----------选中的字母：" + letter);
                for (int i = 0; i < listDatas.size(); i++) {
                    Contact context = listDatas.get(i);
                    if (letter.equals(context.firstLetter)) {
                        // 让列表滚动到指定的位置
                        listView.setSelection(i);
                        return;
                    }
                }
            }
        });
    }
}

